package com.c8a.validation.controller;

import com.c8a.validation.annotation.ValidCollection;
import com.c8a.validation.entity.GroupEmployee;
import com.c8a.validation.entity.Person;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.groups.Default;
import java.util.List;

/**
 * 自定义注解校验
 *
 * @Author C8A
 * @Date 2022-04-24 17:42
 * @Email 1912327276@qq.com
 */

@Validated
@RestController
public class AnnPersonController {

    /**
     * 注解：IsNotEmpty测试
     * {
     * "ids": [1],
     * "name": "22"
     * }
     *
     * @param person
     * @return
     */
    @PostMapping("/valid9")
    public ResponseEntity<Person> valid9(@RequestBody @Valid Person person) {
        return ResponseEntity.ok(person);
    }

    /**
     * 注解：ValidPassword测试
     *
     * {
     *      "ids": [10],
     *      "name": "22",
     *      "password":"C8Aaaa8023."
     * }
     *
     * @param person
     * @return
     */
    @PostMapping("/valid10")
    public ResponseEntity<Person> valid10(@RequestBody @Valid Person person) {
        return ResponseEntity.ok(person);
    }


    /**
     *
     *
     *
     * [
     *     {
     *         "id": 1,
     *         "age": 26,
     *         "title": "初级"
     *     },
     *      {
     *         "id": 2,
     *         "age": 22,
     *         "title": "中级"
     *     }
     * ]
     * @param groupEmployeeList
     * @return
     */
    @PostMapping("/valid11")
    public ResponseEntity<List<GroupEmployee>> valid11(@RequestBody @ValidCollection(grouping = {GroupEmployee.Edit.class, Default.class}, quickFail = true)
                                                               List<GroupEmployee> groupEmployeeList) {
        return ResponseEntity.ok(groupEmployeeList);
    }
}
